package com.store.app.adapter;

import android.content.Context;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.store.app.R;
import com.store.app.bean.ShopCarBean;
import com.store.app.bean.ShoppingInfo;
import com.store.app.bean.WypGoodsBean;
import com.store.app.utils.MoneyUtil;
import com.store.app.utils.Utils;

import java.util.List;

import de.greenrobot.event.EventBus;

public class ShoppingCarAdapter extends BaseAdapter {
    private List<ShoppingInfo> shoppingInfoList;
    private Context context;
    ViewHolder viewHolder = null;
    ShoppingCarItemListViewAdapter temAdapter;

    public ShoppingCarAdapter(List<ShoppingInfo> shoppingInfoList, Context context) {
        this.shoppingInfoList = shoppingInfoList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return shoppingInfoList.size();
    }

    @Override
    public Object getItem(int position) {

        return shoppingInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        Log.v("zyl", "getView:" + position);
        final ShoppingInfo shoppingInfo = shoppingInfoList.get(position);
//        Log.v("zyl", "适配器遍历");
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.shopping_car_item, null);
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.listView = (ListView) convertView.findViewById(R.id.lv);
            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.c_check);
            viewHolder.c_check_lin = (TextView) convertView.findViewById(R.id.c_check_lin);
            viewHolder.tv_warehouse = (TextView) convertView.findViewById(R.id.tv_warehouse);
            viewHolder.tv_distribution_fee = (TextView) convertView.findViewById(R.id.tv_distribution_fee);
            viewHolder.tv_subtotal = (TextView) convertView.findViewById(R.id.tv_subtotal);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.listView.setFocusable(false);
        viewHolder.listView.setEnabled(false);
        viewHolder.listView.setClickable(false);
        viewHolder.tv_warehouse.setText(shoppingInfo.getWarehouse());
        viewHolder.checkBox.setChecked(shoppingInfo.isCheck());
        ShoppingCarItemListViewAdapter temAdapter = new ShoppingCarItemListViewAdapter(shoppingInfo.getBookedBeanList(), context,position);
        viewHolder.listView.setAdapter(temAdapter);
        Utils.Setlistview(viewHolder.listView);
        viewHolder.tvTitle.setText(shoppingInfo.getSupplierName());
        viewHolder.tv_subtotal.setText(planSubtotal(shoppingInfo.getBookedBeanList()));
        if(!MoneyUtil.moneyBigThan(viewHolder.tv_subtotal.getText().toString().trim(),"0")){
            String str =  "0.00"+" <font color='#000000'>(满"+shoppingInfo.getStarting_price()+"元免运费)</font>";
            viewHolder.tv_distribution_fee.setText(Html.fromHtml(str));
        }else if(MoneyUtil.moneyComp(viewHolder.tv_subtotal.getText().toString().trim(),shoppingInfo.getStarting_price())){
            String str = "0.00"+"  <font color='#000000'>(满"+shoppingInfo.getStarting_price()+"元免运费)</font>";
            viewHolder.tv_distribution_fee.setText(Html.fromHtml(str));
        }else{
            String str = shoppingInfo.getShipping_fee()+"  <font color='#000000'>(满"+shoppingInfo.getStarting_price()+"元免运费)</font>";

            viewHolder.tv_distribution_fee.setText(Html.fromHtml(str));
        }


//        viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                Log.v("zyl", "onCheckedChanged:" + isChecked);
//                //通知购物车供应商CheckBox状态变化
//                EventBus.getDefault().post(new ShopCarBean(1, isChecked, position));
////                if(isChecked){
////                    for(int i=0;i<shoppingInfo.getBookedBeanList().size();i++){
////                        shoppingInfo.getBookedBeanList().get(i).setIschecek(true);
////                    }
////                }else{
////                    for(int i=0;i<shoppingInfo.getBookedBeanList().size();i++){
////                        shoppingInfo.getBookedBeanList().get(i).setIschecek(false);
////                    }
////                }
////                notifyDataSetChanged();
//            }
//        });
//
        viewHolder.c_check_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    EventBus.getDefault().post(new ShopCarBean(1, !shoppingInfo.isCheck(), position));
            }
        });

        return convertView;
    }

    /**
     * 计算小计
     * @return
     */
    public String planSubtotal(List<WypGoodsBean> wypGoodsBeanList){
        String sumMoney = "0.00";
        for (int i = 0;i<wypGoodsBeanList.size();i++){
            if(wypGoodsBeanList.get(i).ischecek()){
                sumMoney = MoneyUtil.moneyAdd(sumMoney,MoneyUtil.moneyMul(wypGoodsBeanList.get(i).getLastMoney(),wypGoodsBeanList.get(i).getBuyCount()+""));
            }
        }
        return  sumMoney;
    }
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        Log.v("zyl","子适配器的数量："+listAdapter.getCount());
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            Log.v("zyl", "子控件高度：" + listItem.getMeasuredHeight());
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        Log.v("zyl","算出来的高度为："+ params.height);
        listView.setLayoutParams(params);
    }

    class ViewHolder {
        private TextView tvTitle;//标题
        private ListView listView;//商品列表
        private CheckBox checkBox;
        private TextView c_check_lin;
        private TextView tv_warehouse;
        private TextView tv_distribution_fee;//配送费
        private TextView tv_subtotal;//小计
    }
}
